package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.bean.recommendbean.FootDataEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.FootDataInfoEntity;
import com.qianfeng.wuyang.himalaya.callback.recommendcallback.RecommendFootService;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * Created by WangQing on 2016/12/10.
 */

public class FootRecommendAdapter extends PagerAdapter implements Callback<FootDataEntity>, ViewPager.OnPageChangeListener {
    private List<FootDataInfoEntity> list = new ArrayList<>();
    private Context context;
    private int currentPosition=0;
    private Timer timer;
    private ViewPager viewPager;
    private List<View> viewList = new ArrayList<>();
    private ImageView imgs[] = new ImageView[2];

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    currentPosition++;
                    viewPager.setCurrentItem(currentPosition,false);
                    break;
            }
        }
    };

    public FootRecommendAdapter(Context context, ViewPager viewPager) {
        this.context = context;
        this.viewPager = viewPager;
        this.viewPager.addOnPageChangeListener(this);
        Log.d("1620","foot");
    }

    @Override
    public int getCount() {
        Log.d("1620","foot:getcount"+list.size());
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // http://adse.ximalaya.com/ting?device=android&name=cata_index_banner&network=wifi&operator=0&version=4.3.98

        RecommendFootService service = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RecommendFootService.class);
        service.getFootList("android","cata_index_banner","wifi",0,"4.3.98").enqueue(this);

        Log.d("1620","isview");

        return view==object;
    }

    //开启子线程
    public void startTimer(){
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 1;
                handler.sendEmptyMessage(message.what);
            }
        }, 3000);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        Log.d("1620","inst");
        return viewList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }

    @Override
    public void onResponse(Call<FootDataEntity> call, Response<FootDataEntity> response) {
        if (response.body()!=null) {
            list.addAll(response.body().getData());
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCover()!=null) {
                    ImageView imageView= new ImageView(context);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    Picasso.with(context).load(list.get(i).getCover()).into(imageView);
                    if (i==0) {
                        imgs[0]=imageView;
                    }
                    if (i==list.size()-1) {
                        imgs[1]=imageView;
                    }
                    viewList.add(imageView);
                }
            }
            viewList.add(0,imgs[1]);
            viewList.add(list.size()+1,imgs[0]);

            startTimer();
        }
    }

    @Override
    public void onFailure(Call<FootDataEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(context,t.getMessage(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_IDLE:
                if (currentPosition == 0) {
                    viewPager.setCurrentItem(list.size()-2,false);
                }
                if (currentPosition == list.size()-1) {
                    viewPager.setCurrentItem(1,false);
                }
                if (timer==null) {
                    currentPosition--;
                    startTimer();
                }
                break;
            case ViewPager.SCROLL_STATE_DRAGGING:
                if (timer!=null) {
                    timer.cancel();
                    timer = null;
                }
                break;

        }
    }
}
